Garbage collection is the automated reclamation of system memory space after its last use by a programme. A number of examples of garbage collecting techniques are discussed in "Garbage Collection: Algorithms for Automatic Dynamic Memory Management" by R. Jones et al, pub. John Wiley & Sons 1996, ISBN 0-471-94148-4, at pages 1 to 18, and "Uniprocessor Garbage Collection Techniques" by P. R. Wilson, Proceedings of the 1992 International Workshop on Memory Management, St. Malo, France, September 1992. Whilst the storage requirements of many computer programs are simple and predictable, with memory allocation and recovery being handled by the programmer or a compiler, there is a trend toward functional languages having more complex patterns of execution such that the lifetimes of particular data structures can no longer be determined prior to run-time and hence automated reclamation of this storage, as the program runs, is essential.
A common feature of a number of garbage collection reclamation techniques, as described in the above-mentioned Wilson reference, is incrementally traversing the data structure formed by referencing pointers carried by separately stored data objects. The technique involves first marking all stored objects that are still reachable by other stored objects or from external locations by tracing a path or paths through the pointers linking data objects. This may be followed by sweeping or compacting the memory--that is to say examining every object stored in the memory to determine the unmarked objects whose space may then be reclaimed.
In many cases, garbage collection is a system-wide task which operates on a single global heap, that is to say a single memory area where data structures or objects are stored in no specific order--only with regard to whether a particular space is large enough to hold a particular object. All tracing garbage collection systems include a mechanism for traversing the data structure within the heap. A mark stack or queue is used to store references to the objects pending traversal. However, in the worst case situation, the size of the marking data structure can be comparable with the size of the heap. As the purpose of the garbage collector is to reclaim memory, it is unacceptable for the marking process to require more memory than a small static amount. The Wilson reference identifies a number of conventional solutions, including dedicating a field in each allocated object for a pointer to the next in the marking list. This guarantees a static overhead by allowing for the worst-case situation, where all objects are on the marking list, and is therefore wasteful in memory. Another technique allocates a fixed-size marking structure, and uses the marking structure until is becomes full, following which the marking list is processed without placing new items back on it. Memory then needs to be scanned to find the overflow items. This compromise solution has a poor worst-case performance because large areas of memory may need to be repeatedly scanned for overflowed data. A third technique uses pointer reversal to store pointers back up the marking tree from the current node. Although no extra space is required, because the heap objects are in a non-usable state whilst the algorithm is running, this algorithm cannot be used incrementally. Of the three techniques, the second is preferred as it is not as wasteful of memory as the first and, unlike the third, it does not require the program execution to halt for extended periods which the complete garbage collection procedure is implemented.